package com.yinNuo.collectWool.quartz.controller;

import com.yinNuo.collectWool.core.Response;
import com.yinNuo.collectWool.quartz.common.Constants;
import com.yinNuo.collectWool.quartz.entity.SysJob;
import com.yinNuo.collectWool.quartz.exception.TaskException;
import com.yinNuo.collectWool.quartz.service.CustomJobService;
import com.yinNuo.collectWool.quartz.service.ISysJobService;
import com.yinNuo.collectWool.quartz.util.CronUtils;
import com.yinNuo.collectWool.quartz.util.ScheduleUtils;
import com.yinNuo.collectWool.quartz.util.StringUtils;
import com.yinNuo.collectWool.quartz.vo.SysJobVo;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* create by: yinNuo
* create at: 2022/1/8 下午7:09
* description: 调度任务信息操作处理
*/
@RestController
@RequestMapping("/quartz/job")
public class SysJobController {
    @Autowired
    private ISysJobService jobService;

    @Resource
    private CustomJobService customJobService;

    /**
     * 查询定时任务列表
     */
    @GetMapping("/list")
    public Response list(SysJobVo sysJobVo) {
        Map<String,Object> resultMap =new HashMap<>();
        List<SysJob> list = jobService.selectJobList(sysJobVo);
        int count = jobService.jobListCount(sysJobVo);
        resultMap.put("list", list);
        resultMap.put("count", count);
        return Response.ok(resultMap);
    }

    /**
     * 获取定时任务详细信息
     */
    @GetMapping(value = "/getJobById/{jobId}")
    public Response getJobById(@PathVariable("jobId") Long jobId) {
        return Response.ok(customJobService.getById(jobId));
    }

    /**
     * 获取定时任务
     */
    //@GetMapping(value = "/getInfo/{jobId}")
    //public Response getInfo(@PathVariable("jobId") Long jobId) {
    //    return Response.ok(jobService.selectJobById(jobId));
    //}

    /**
     * 新增定时任务
     */
    @PostMapping("/addJob")
    public Response add(@RequestBody SysJob job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) {
            return Response.failed("新增任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        job.setCreateBy("1");
        return Response.ok(jobService.insertJob(job));
    }

    /**
     * 修改定时任务
     */
    @PutMapping("/edit")
    public Response edit(@RequestBody SysJob job) throws SchedulerException, TaskException {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        } else if (!ScheduleUtils.whiteList(job.getInvokeTarget())) {
            return Response.failed("修改任务'" + job.getJobName() + "'失败，目标字符串不在白名单内");
        }
        job.setUpdateBy("1");
        return Response.ok(jobService.updateJob(job));
    }

    /**
     * 定时任务状态修改
     */
    @PutMapping("/changeStatus")
    public Response changeStatus(@RequestBody SysJob job) throws SchedulerException {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return Response.ok(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PutMapping("/run")
    public Response run(@RequestBody SysJob job) throws SchedulerException {
        jobService.run(job);
        return Response.ok();
    }

    /**
     * 删除定时任务
     */
    @GetMapping("/delete")
    public Response remove( SysJobVo sysJobVo) throws SchedulerException, TaskException {
        System.out.println(sysJobVo);
        String[] split = sysJobVo.getJobId().split(",");
        Long[] longIds = new Long[split.length];
        for (int idx = 0; idx < split.length; idx++) {
            longIds[idx] = Long.parseLong(split[idx]);
        }
        jobService.deleteJobByIds(longIds);
        return Response.ok();
    }
}
